package com.itheima.ticket.mai4;

import java.util.concurrent.locks.ReentrantLock;

public class Ticket implements Runnable {
    private int count = 100;
    private ReentrantLock lock= new ReentrantLock();

    @Override
    public void run() {
        //完成卖票操作
        while (true) {
            String name = Thread.currentThread().getName();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //获取锁
            lock.lock();
            if (count > 0) {
                count--;
                System.out.println(name + "卖了一张还剩" + count + "张票");
            } else {
                break;
            }
            //释放锁
            lock.unlock();
        }
    }
}
